#include "http.hpp"

//长连接测试
void test_cli_1()
{
    Socket cli_sock;
    cli_sock.CreateClient("127.0.0.1",8989);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1) {
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_sock.Recv(buf, 1023));
        LOG(LogLevel::DEBUG) << buf ;
        sleep(3);
    }
    cli_sock.Close();
}

//超时测试
void test_cli_2()
{
    Socket cli_sock;
    cli_sock.CreateClient("127.0.0.1",8989);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1) {
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_sock.Recv(buf, 1023));
        LOG(LogLevel::DEBUG) << buf ;
        sleep(15);
    }
    cli_sock.Close();
}


//数据处理出错测试
void test_cli_3()
{
    Socket cli_sock;
    cli_sock.CreateClient("127.0.0.1",8989);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 100\r\n\r\n";
    while(1) {
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_sock.Recv(buf, 1023));
        LOG(LogLevel::DEBUG) << buf ;
        sleep(3);
    }
    cli_sock.Close();
}


void test_cli_4()
{
    signal(SIGCHLD, SIG_IGN);
    for (int i = 0; i < 10; i++) {
        pid_t pid = fork();
        if (pid < 0) {
            return ;
        }else if (pid == 0) {
            LOG(LogLevel::DEBUG) << "创建一个子进程" ;
            Socket cli_sock;
            cli_sock.CreateClient("127.0.0.1",8989);
            std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
            while(1) {
                assert(cli_sock.Send(req.c_str(), req.size()) != -1);
                char buf[1024] = {0};
                assert(cli_sock.Recv(buf, 1023));
                LOG(LogLevel::DEBUG) << buf ;
            }
            cli_sock.Close();
            exit(0);
        }
    }
    while(1) sleep(1);
}

void test_cli_5()
{
    Socket cli_sock;
    cli_sock.CreateClient("127.0.0.1",8989);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1) {
        assert(cli_sock.Send(req.c_str(), req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_sock.Recv(buf, 1023));
        LOG(LogLevel::DEBUG) << buf ;
        sleep(3);
    }
    cli_sock.Close();
}

void test_cli_6()
{
    Socket cli_sock;
    cli_sock.CreateClient("127.0.0.1",8989);
    std::string req = "PUT /1234.txt HTTP/1.1\r\nConnection: keep-alive\r\n";
    std::string body;
    Util::ReadFile("./hello.txt", &body);
    req += "Content-Length: " + std::to_string(body.size()) + "\r\n\r\n";
    req += body;
    assert(cli_sock.Send(req.c_str(), req.size()) != -1);
    //assert(cli_sock.Send(body.c_str(), body.size()) != -1);
    char buf[1024] = {0};
    assert(cli_sock.Recv(buf, 1023));
    LOG(LogLevel::DEBUG) << buf ;
    sleep(3);
    cli_sock.Close();
}

int main()
{
    //test_cli_1();
    //test_cli_2();
    //test_cli_3();
    //test_cli_4();
    //test_cli_5();
    test_cli_6();
    return 0;
}